package com.logistics.utils.socket;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.logistics.utils.tool.StringUtils;

import java.util.concurrent.ConcurrentHashMap;

public class MenConns {

    public static ConcurrentHashMap<String, UserBean> userBeans = new ConcurrentHashMap<>();

    public static void sendText(String eventName, Object data){
        userBeans.forEach((key, value) -> {
            try {
                ObjectMapper mapper = new ObjectMapper();
                // 禁用去除null字段的特性
                mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
                String message = mapper.writeValueAsString(EventData.build(eventName, data));
                if(StringUtils.isNotNull(value.getPcSession())){
                    value.getPcSession().getSession().getAsyncRemote().sendText(message);
                }else if(StringUtils.isNotNull(value.getPhoneSession())){
                    value.getPhoneSession().getSession().getAsyncRemote().sendText(message);
                }
            }catch (JsonProcessingException e){
                throw new RuntimeException(e);
            }
        });
    }
}
